package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import types.ConsoleType;
import model.Absence;
import controller.Console;
import controller.DeleteAbsence;
import controller.TableModelTodayAbsence;
import controller.TableModelWeekAbsence;

@SuppressWarnings("serial")
public class PanelDASboard extends JPanel implements Runnable{

	private static PanelDASboard single;
	
	   private Thread t;

	JTable todayAbsence = new JTable(new TableModelTodayAbsence());
	JScrollPane todayJSP = new JScrollPane(todayAbsence);

	JTable weekAbsence = new JTable(new TableModelWeekAbsence());
	JScrollPane weekJSP = new JScrollPane(weekAbsence);

	private JButton addAB = new JButton("Nieuwe absentie");
	private JButton EditAB = new JButton("Wijzig");
	private JButton DeleteAB = new JButton("Verwijder");
	private JButton DeleteConfirmAB = new JButton("Bevestigen");
	private JButton JBAllAbsences = new JButton("Alle Absenties");

	private JLabel TitleTodayJSP = new JLabel("Vandaag:");
	private JLabel TitleWeekJSP = new JLabel("Deze week:");
	String message = null;
	ConsoleType type = null;

	public JTable selectedTable;

	public PanelDASboard() {
		setBounds(0, 0, 860, 720);
		setLayout(null);

		todayAbsence.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				setSelectedTable(todayAbsence);
			}
		});
		weekAbsence.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				setSelectedTable(weekAbsence);
			}
		});

		TitleTodayJSP.setFont(new Font("Serif", Font.PLAIN, 18));
		TitleTodayJSP.setBounds(280, 65, 200, 40);
		todayJSP.setBounds(280, 100, 763, 150);
		addAB.setBounds(1, 100, 140, 40);

		JBAllAbsences.setBounds(1, 300, 140, 40);

		TitleWeekJSP.setFont(new Font("Serif", Font.PLAIN, 18));
		TitleWeekJSP.setBounds(280, 265, 250, 40);
		weekJSP.setBounds(280, 300, 763, 300);

		addAB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				PanelHolder mf = PanelHolder.getInstance();
				mf.remove(PanelDASboard.getInstance());
				mf.add(PanelAddAbsence.getInstance());
				PanelAddAbsence.getInstance().setpreviousPage(1);
				mf.repaint();
				mf.validate();
			}
		});

		JBAllAbsences.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				PanelHolder ph = PanelHolder.getInstance();
				ph.remove(PanelDASboard.getInstance());
				ph.add(PanelAbsenceList.getInstance());
				ph.repaint();
				ph.validate();

			}
		});
		EditAB.setBounds(1, 150, 140, 40);
		EditAB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				if (todayAbsence.getSelectedColumnCount() <= 0
						&& weekAbsence.getSelectedColumnCount() <= 0) {
					message = "Selecteer een absentie";
					type = ConsoleType.ERROR;

				} else {
					/* checks selected column */
					if (!(todayAbsence.getSelectedColumnCount() <= 0)) {
						Absence A = ((TableModelTodayAbsence) todayAbsence
								.getModel()).getSelectedRowObject(todayAbsence
								.getSelectedRow());

						PanelEditAbsence PEA = new PanelEditAbsence(A);
						PEA.updateAbsenceEditor(A);
						PEA.getInvallerCombo().setSelectedItem(
								A.getAbsence_Substitude());
						PEA.getUitvallerCombo().setSelectedItem(
								A.getAbsence_Teacher());
						PEA.getRedenCombo().setSelectedItem(
								A.getAbsence_Reason());
						PEA.getJTNote().setText(A.getAbsence_Note());
						PEA.getJSStartDate().setValue(A.getAbsence_StartDate());
						PEA.getJSEndDate().setValue(A.getAbsence_EndDate());
						PEA.getJTid()
								.setText(String.valueOf(A.getAbsence_Id()));
						PEA.getJThours().setText(
								String.valueOf((A.getAbsence_Hours())));

						PanelHolder Ph = PanelHolder.getInstance();
						Ph.remove(PanelDASboard.getInstance());
						Ph.add(PEA);
						Ph.repaint();
						Ph.validate();
					} else {

						Absence A1 = ((TableModelWeekAbsence) weekAbsence
								.getModel()).getSelectedRowObject(weekAbsence
								.getSelectedRow());

						PanelEditAbsence PEA = new PanelEditAbsence(A1);
						PEA.updateAbsenceEditor(A1);

						PEA.getInvallerCombo().setSelectedItem(
								A1.getAbsence_Substitude());
						PEA.getUitvallerCombo().setSelectedItem(
								A1.getAbsence_Teacher());
						PEA.getRedenCombo().setSelectedItem(
								A1.getAbsence_Reason());
						PEA.getJTNote().setText(A1.getAbsence_Note());
						PEA.getJSStartDate()
								.setValue(A1.getAbsence_StartDate());
						PEA.getJSEndDate().setValue(A1.getAbsence_EndDate());
						PEA.getJTid().setText(
								String.valueOf(A1.getAbsence_Id()));
						PEA.getJThours().setText(
								String.valueOf((A1.getAbsence_Hours())));

						PanelHolder Ph = PanelHolder.getInstance();
						Ph.remove(PanelDASboard.getInstance());
						Ph.add(PEA);
						Ph.repaint();
						Ph.validate();

					}
				}
				Console.getInstance().setConsole(message, type);
			}

		});

		todayAbsence.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {

			}

			@Override
			public void focusGained(FocusEvent e) {
				DeleteAB.setVisible(true);
				DeleteConfirmAB.setVisible(false);

			}
		});

		weekAbsence.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {

			}

			@Override
			public void focusGained(FocusEvent e) {
				DeleteAB.setVisible(true);
				DeleteConfirmAB.setVisible(false);

			}
		});

		DeleteAB.setBounds(1, 200, 140, 40);
		DeleteConfirmAB.setBounds(1, 200, 140, 40);
		DeleteConfirmAB.setBackground(Color.RED);
		DeleteConfirmAB.setVisible(false);
		DeleteAB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				DeleteAB.setVisible(false);
				DeleteConfirmAB.setVisible(true);

			}
		});

		DeleteConfirmAB.addActionListener(new DeleteAbsence(this));
		DeleteConfirmAB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				DeleteAB.setVisible(true);
				DeleteConfirmAB.setVisible(false);

			}
		});

		add(weekJSP);
		add(todayJSP);
		add(TitleWeekJSP);
		add(TitleTodayJSP);
		add(DeleteAB);
		add(DeleteConfirmAB);
		add(EditAB);
		add(addAB);
		add(JBAllAbsences);

	}
	

	

	   public void run ()
	   {
	      while (t == Thread.currentThread ())
	      {
	         System.out.println("current thread");
	          
	       
	      }
	   }

	   public void start ()
	   {
	      if (t == null)
	      {
	         t = new Thread (this);
	         t.start ();
	      }
	   }

	   public void stop ()
	   {
	      if (t != null)
	         t = null;
	   }


	public static PanelDASboard getInstance() {
		if (single == null) {
			single = new PanelDASboard();
		}
		return single;
	}

	public void selfDestroy() {
		single = null;
	}

	public JTable getAbsenceListToday() {
		return todayAbsence;
	}

	public JTable getAbsenceListWeek() {
		return weekAbsence;
	}

	public JTable getSelectedTable() {
		return selectedTable;
	}

	public void setSelectedTable(JTable selectedTable) {
		this.selectedTable = selectedTable;
	}

	public JButton getJBAllAbsences() {
		return JBAllAbsences;
	}

	public void resetDeleteButtons() {
		DeleteAB.setVisible(true);
		DeleteConfirmAB.setVisible(false);
	}

	public void setJBAllAbsences(JButton jBAllAbsences) {
		JBAllAbsences = jBAllAbsences;
	}



}
